import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'app-select-logistics',
  templateUrl: './select-logistics.component.html',
  styleUrls: ['./select-logistics.component.scss']
})
export class SelectLogisticsComponent implements OnInit {
  @Input() data: any;
  list: any[] = [];
  constructor() { }
  get selectedItem() {
    return this.list.find(item => item.checked) || null;
  }
  ngOnInit(): void {
    // Mock data
    this.list = [
      { logisticsCode: 'SF', companyName: '顺丰速运', contactName: '王顺丰', contactPhone: '95338', address: '深圳市南山区', checked: false },
      { logisticsCode: 'JD', companyName: '京东物流', contactName: '刘京东', contactPhone: '950616', address: '北京市亦庄经济开发区', checked: false },
      { logisticsCode: 'YT', companyName: '圆通速递', contactName: '张圆通', contactPhone: '95554', address: '上海市青浦区', checked: false },
      { logisticsCode: 'ZT', companyName: '中通快递', contactName: '李中通', contactPhone: '95311', address: '上海市', checked: false },
    ];
    // 如果传入了当前选中的物流公司，设置为默认选中
    if (this.data && this.data.currentLogistics) {
      this.list.forEach(item => {
        item.checked = item.logisticsCode === this.data.currentLogistics.logisticsCode &&
                      item.contactPhone === this.data.currentLogistics.contactPhone;
      });
    }
  }
  radio(event: Event, rowItem: any) {
    if (rowItem.checked) {
      // 已选中，点击则取消
      rowItem.checked = false;
      (event.target as HTMLInputElement).checked = false;
    } else {
      // 只允许单选
      this.list.forEach(item => item.checked = false);
      rowItem.checked = true;
    }
  }

  clearSelection() {
    this.list.forEach(item => item.checked = false);
  }
}

